package com.example.user

import android.content.DialogInterface
import android.graphics.Color
import android.os.Bundle
import android.text.SpannableStringBuilder
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.lib_base.mvvm.BaseActivity
import com.example.lib_base.net.EventBus
import com.example.lib_base.room.AppDbUtils
import com.example.user.databinding.ActivityLoginPwdBinding
@Route(path = "/user/LoginPwdActivity")
class LoginPwdActivity : BaseActivity<ActivityLoginPwdBinding, UserViewModel>() {
    override fun initView() {
        //富文本
        val builder = SpannableStringBuilder(binding.tvFwb.text.toString())
        //设置文字颜色
        builder.setSpan(
            ForegroundColorSpan(Color.RED),
            7,
            12,
            SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE
        )
        //点击事件
        val clickableSpan = object : ClickableSpan() {
            override fun onClick(widget: View) {
                showWindow("这个是用户政策这个是用户政策这个是用户政策这个是用户政策这个是用户政策" +
                        "这个是用户政策这个是用户政策这个是用户政策这个是用户政策这个是用户政策" +
                        "这个是用户政策这个是用户政策这个是用户政策这个是用户政策这个是用户政策" +
                        "这个是用户政策这个是用户政策这个是用户政策这个是用户政策")
            }
        }
        val clickableSpan2 = object : ClickableSpan() {
            override fun onClick(widget: View) {
                showWindow("这个是隐私协议")
            }
        }
        builder.setSpan(clickableSpan, 7, 13, SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE)
        builder.setSpan(clickableSpan2,14,20,SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE)
        binding.tvFwb.movementMethod = LinkMovementMethod.getInstance()
        binding.tvFwb.text = builder
        //默认登录按钮不可用，当复选框选中时，可用
        binding.btnLoginPwd.isEnabled = false
        binding.cb.setOnCheckedChangeListener { buttonView, isChecked ->
            //监听cb的选中状态
            binding.btnLoginPwd.isEnabled = isChecked
        }
        //点击实现登录
        binding.btnLoginPwd.setOnClickListener {
            val etName = binding.etName.text.toString()
            val etPwd = binding.etPwd.text.toString()
            if(etName.isEmpty()||etPwd.isEmpty()){
                ToastUtils.showLong("用户名和密码均不能为空")
                return@setOnClickListener
            }
            if(!RegexUtils.isMobileExact(etName)){
               ToastUtils.showLong("用户名格式不正确，应为手机号")
               return@setOnClickListener
            }
            if(!RegexUtils.isMatch("^[\\w]{8,20}$",etPwd)){
                ToastUtils.showLong("密码格式不正确")
                return@setOnClickListener
            }
            viewModel.loginPwd(etName,etPwd)
        }
        //点击验证码登录，事件总线发送事件
      /*  binding.tvGologin.setOnClickListener {
            EventBus.instance.sendEvent("gologin","点击了验证码登录控件")
        }*/

    }
    //富文本弹框
    private fun showWindow(s: String) {
        AlertDialog.Builder(this)
            .setTitle("提示")
            .setMessage(s)
            .setNegativeButton("不同意"){
                a,b->
                binding.cb.isChecked = false
            }
            .setPositiveButton("同意",object :DialogInterface.OnClickListener{
                override fun onClick(dialog: DialogInterface?, which: Int) {
                    binding.cb.isChecked = true
                }
            })
            .show()
    }

    override fun initData() {
        viewModel.loginPwdSuccess.observe(this){
            ToastUtils.showLong(it.message)
            if(it.code==200){
                SPUtils.getInstance().put("token",it.data.token)
                SPUtils.getInstance().put("phone",it.data.phone)
                AppDbUtils.getAppDb().getUserDao().insert(it.data)
                ARouter.getInstance().build("/app/ShopActivity").navigation()
            }
        }
        //事件总线之  接收事件
       /* EventBus.instance.getEvent<String>("gologin").observe(this){
            ToastUtils.showLong(it)
        }*/
    }

    override fun getViewModel(): Class<UserViewModel> = UserViewModel::class.java

    override fun getLayout(): Int = R.layout.activity_login_pwd

}


